package io.terminus.spzx.manager.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.consts.UserInfoConstant;
import io.terminus.spzx.manager.service.SysMenuService;
import io.terminus.spzx.manager.service.SysUserService;
import io.terminus.spzx.manager.service.ValidateCodeService;
import io.terminus.spzx.model.dto.system.LoginDto;
import io.terminus.spzx.model.entity.system.SysUser;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import io.terminus.spzx.model.vo.system.LoginVo;
import io.terminus.spzx.model.vo.system.SysMenuVo;
import io.terminus.spzx.model.vo.system.ValidateCodeVo;
import io.terminus.spzx.utils.AuthContextUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lichunlong
 * @desc 登录首页Controller
 */
@RestController
@Tag(name = "用户接口")
@RequiredArgsConstructor
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    private final ValidateCodeService validateCodeService;

    private final SysUserService sysUserService;

    private final SysMenuService sysMenuService;

    /**
     * 生成图片验证码
     *
     * @return 图片验证码VO
     */
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCode = validateCodeService.generateValidateCode();

        return Result.build(validateCode, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户登录
     *
     * @return 通用返回对象
     */
    @PostMapping("login")
    @Operation(summary = "用户登录")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);

        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    /**
     * 用户退出
     *
     * @param token 登录Token
     * @return 通用对象
     */
    @GetMapping(value = "/logout")
    public Result<Void> logout(@RequestHeader(name = UserInfoConstant.USERINFO_TOKEN_REQUEST_HEADER) String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 获取当前登录用户信息
     *
     * @param token 登录Token
     * @return 用户信息
     */
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = UserInfoConstant.USERINFO_TOKEN_REQUEST_HEADER) String token) {
        /*
         * 1、根据token查询redis获取用户信息
         * 方式一: SysUser sysUser = sysUserService.getUserInfo(token);
         */
        SysUser sysUser = AuthContextUtil.get();

        // 2、用户信息返回
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户可以操作的菜单
     *
     * @return 可以操作的菜单列表
     */
    @GetMapping("/menus")
    public Result<List<SysMenuVo>> menus() {
        List<SysMenuVo> list = sysMenuService.findMenusByUserId();

        return Result.build(list, ResultCodeEnum.SUCCESS);
    }
}
